package com.mysiteforme.admin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.mysiteforme.admin.entity.ElectricityPushFreemaker;
import com.mysiteforme.admin.service.ElectricityPushFreemakerService;
import com.baomidou.mybatisplus.plugins.Page;
import com.mysiteforme.admin.util.LayerData;
import com.mysiteforme.admin.util.RestResponse;
import com.mysiteforme.admin.annotation.SysLog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import javax.servlet.ServletRequest;
import java.util.Map;

/**
 * <p>
 * 微信推送模板  前端控制器
 * </p>
 *
 * @author wangl
 * @since 2019-10-22
 */
@Controller
@RequestMapping("/admin/electricityPushFreemaker")
public class ElectricityPushFreemakerController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ElectricityPushFreemakerController.class);

    @Autowired
    private ElectricityPushFreemakerService electricityPushFreemakerService;

    @GetMapping("list")
    @SysLog("跳转微信推送模板列表")
    public String list(){
        return "/admin/electricityPushFreemaker/list";
    }

    @PostMapping("list")
    @ResponseBody
    @SysLog("请求微信推送模板列表数据")
    public LayerData<ElectricityPushFreemaker> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<ElectricityPushFreemaker> layerData = new LayerData<>();
        EntityWrapper<ElectricityPushFreemaker> wrapper = new EntityWrapper<>();
        if(!map.isEmpty()){
            String freemakerName = (String) map.get("freemakerName");
            if(StringUtils.isNotBlank(freemakerName)) {
                wrapper.like("freemaker_name",freemakerName);
            }else{
                map.remove("freemakerName");
            }

        }
        Page<ElectricityPushFreemaker> pageData = electricityPushFreemakerService.selectPage(new Page<>(page,limit),wrapper);
        for (ElectricityPushFreemaker electricityPushFreemaker : pageData.getRecords()) {
            electricityPushFreemaker.setStringId(String.valueOf(electricityPushFreemaker.getId()));
        }
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }

    @GetMapping("add")
    @SysLog("跳转新增微信推送模板页面")
    public String add(){
        return "/admin/electricityPushFreemaker/add";
    }

    @PostMapping("add")
    @SysLog("保存新增微信推送模板数据")
    @ResponseBody
    public RestResponse add(ElectricityPushFreemaker electricityPushFreemaker){
        if(StringUtils.isBlank(electricityPushFreemaker.getFreemakerName())){
            return RestResponse.failure("模板名称不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemaker.getFreemakerId())){
            return RestResponse.failure("模板id不能为空");
        }
        EntityWrapper<ElectricityPushFreemaker> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("freemaker_id",electricityPushFreemaker.getFreemakerId());
        ElectricityPushFreemaker pushFreemaker= electricityPushFreemakerService.selectOne(entityWrapper);
        if(pushFreemaker == null) {
            electricityPushFreemakerService.insert(electricityPushFreemaker);
            return RestResponse.success();
        }else{
            return RestResponse.failure("该模板已存在");
        }
    }

    @GetMapping("edit")
    @SysLog("跳转编辑微信推送模板页面")
    public String edit(Long id,Model model){
        ElectricityPushFreemaker electricityPushFreemaker = electricityPushFreemakerService.selectById(id);
        model.addAttribute("electricityPushFreemaker",electricityPushFreemaker);
        return "/admin/electricityPushFreemaker/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    @SysLog("保存编辑微信推送模板数据")
    public RestResponse edit(ElectricityPushFreemaker electricityPushFreemaker){
        if(StringUtils.isBlank(electricityPushFreemaker.getFreemakerId())){
            return RestResponse.failure("模板id不能为空");
        }
        if(StringUtils.isBlank(electricityPushFreemaker.getFreemakerName())){
            return RestResponse.failure("模板名称不能为空");
        }
        EntityWrapper<ElectricityPushFreemaker> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("freemaker_id",electricityPushFreemaker.getFreemakerId());
        ElectricityPushFreemaker pushFreemaker= electricityPushFreemakerService.selectOne(entityWrapper);
        if(pushFreemaker!=null) {
            electricityPushFreemakerService.updateById(electricityPushFreemaker);
            return RestResponse.success();
        }
        return RestResponse.failure("更新失败");
    }

    @PostMapping("delete")
    @ResponseBody
    @SysLog("删除微信推送模板数据")
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        ElectricityPushFreemaker electricityPushFreemaker = electricityPushFreemakerService.selectById(id);
        electricityPushFreemakerService.updateById(electricityPushFreemaker);
        return RestResponse.success();
    }

}
